home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Graphics Programming (2nd Edition) / Visual Basic Graphics Programming 2nd Edition.iso / OldSrc / CH8 / SRC / RNDTREE.FRM (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1996-03-05  |  9.0 KB  |  307 lines

  1. VERSION 4.00
  2. Begin VB.Form RndTreeForm 
  3.    Caption         =   "Randomized Tree"
  4.    ClientHeight    =   4335
  5.    ClientLeft      =   1140
  6.    ClientTop       =   1335
  7.    ClientWidth     =   7470
  8.    Height          =   5025
  9.    Left            =   1080
  10.    LinkTopic       =   "Form1"
  11.    ScaleHeight     =   289
  12.    ScaleMode       =   3  'Pixel
  13.    ScaleWidth      =   498
  14.    Top             =   705
  15.    Width           =   7590
  16.    Begin VB.TextBox RndDThetaText 
  17.       Height          =   285
  18.       Left            =   1320
  19.       MaxLength       =   3
  20.       TabIndex        =   14
  21.       Text            =   "10"
  22.       Top             =   1800
  23.       Width           =   615
  24.    End
  25.    Begin VB.CheckBox BendCheck 
  26.       Caption         =   "Bend Branches"
  27.       Height          =   255
  28.       Left            =   240
  29.       TabIndex        =   13
  30.       Top             =   2640
  31.       Width           =   1455
  32.    End
  33.    Begin VB.TextBox MaxBranchesText 
  34.       Height          =   285
  35.       Left            =   1320
  36.       MaxLength       =   3
  37.       TabIndex        =   11
  38.       Text            =   "3"
  39.       Top             =   360
  40.       Width           =   615
  41.    End
  42.    Begin VB.TextBox RndScaleText 
  43.       Height          =   285
  44.       Left            =   1320
  45.       MaxLength       =   5
  46.       TabIndex        =   9
  47.       Text            =   "0.20"
  48.       Top             =   1080
  49.       Width           =   615
  50.    End
  51.    Begin VB.TextBox DThetaText 
  52.       Height          =   285
  53.       Left            =   1320
  54.       MaxLength       =   3
  55.       TabIndex        =   2
  56.       Text            =   "36"
  57.       Top             =   1440
  58.       Width           =   615
  59.    End
  60.    Begin VB.TextBox ScaleText 
  61.       Height          =   285
  62.       Left            =   1320
  63.       MaxLength       =   5
  64.       TabIndex        =   1
  65.       Text            =   "0.75"
  66.       Top             =   720
  67.       Width           =   615
  68.    End
  69.    Begin VB.TextBox LevelText 
  70.       Height          =   285
  71.       Left            =   1320
  72.       MaxLength       =   3
  73.       TabIndex        =   0
  74.       Text            =   "5"
  75.       Top             =   0
  76.       Width           =   615
  77.    End
  78.    Begin VB.CheckBox TaperCheck 
  79.       Caption         =   "Taper Branches"
  80.       Height          =   255
  81.       Left            =   240
  82.       TabIndex        =   3
  83.       Top             =   2280
  84.       Width           =   1455
  85.    End
  86.    Begin VB.PictureBox Canvas 
  87.       AutoRedraw      =   -1  'True
  88.       Height          =   4335
  89.       Left            =   2040
  90.       ScaleHeight     =   285
  91.       ScaleMode       =   3  'Pixel
  92.       ScaleWidth      =   357
  93.       TabIndex        =   6
  94.       Top             =   0
  95.       Width           =   5415
  96.    End
  97.    Begin VB.CommandButton CmdGo 
  98.       Caption         =   "Go"
  99.       Default         =   -1  'True
  100.       Height          =   495
  101.       Left            =   600
  102.       TabIndex        =   4
  103.       Top             =   3120
  104.       Width           =   735
  105.    End
  106.    Begin VB.Label Label1 
  107.       Caption         =   "Rnd DTheta"
  108.       Height          =   255
  109.       Index           =   3
  110.       Left            =   0
  111.       TabIndex        =   15
  112.       Top             =   1800
  113.       Width           =   1335
  114.    End
  115.    Begin VB.Label Label1 
  116.       Caption         =   "Max Branches"
  117.       Height          =   255
  118.       Index           =   5
  119.       Left            =   0
  120.       TabIndex        =   12
  121.       Top             =   360
  122.       Width           =   1335
  123.    End
  124.    Begin VB.Label Label1 
  125.       Caption         =   "Rnd Scale"
  126.       Height          =   255
  127.       Index           =   4
  128.       Left            =   0
  129.       TabIndex        =   10
  130.       Top             =   1080
  131.       Width           =   1335
  132.    End
  133.    Begin MSComDlg.CommonDialog FileDialog 
  134.       Left            =   720
  135.       Top             =   3720
  136.       _Version        =   65536
  137.       _ExtentX        =   847
  138.       _ExtentY        =   847
  139.       _StockProps     =   0
  140.       CancelError     =   -1  'True
  141.    End
  142.    Begin VB.Label Label1 
  143.       Caption         =   "DTHETA"
  144.       Height          =   255
  145.       Index           =   2
  146.       Left            =   0
  147.       TabIndex        =   8
  148.       Top             =   1440
  149.       Width           =   1335
  150.    End
  151.    Begin VB.Label Label1 
  152.       Caption         =   "LENGTH_SCALE"
  153.       Height          =   255
  154.       Index           =   1
  155.       Left            =   0
  156.       TabIndex        =   7
  157.       Top             =   720
  158.       Width           =   1335
  159.    End
  160.    Begin VB.Label Label1 
  161.       Caption         =   "Level"
  162.       Height          =   255
  163.       Index           =   0
  164.       Left            =   0
  165.       TabIndex        =   5
  166.       Top             =   0
  167.       Width           =   1335
  168.    End
  169.    Begin VB.Menu mnuFile 
  170.       Caption         =   "&File"
  171.       Begin VB.Menu mnuFileExit 
  172.          Caption         =   "E&xit"
  173.       End
  174.    End
  175. Attribute VB_Name = "RndTreeForm"
  176. Attribute VB_Creatable = False
  177. Attribute VB_Exposed = False
  178. Option Explicit
  179. Const PI = 3.14159
  180. Const PI_2 = PI / 2
  181. Const PI_5 = PI / 5
  182. Dim LengthScale As Single
  183. Dim RndScale As Single
  184. Dim DTheta As Single
  185. Dim RndDTheta As Single
  186. Dim MaxBranches As Integer
  187. Dim TheLevel As Integer
  188. Dim StartX As Integer
  189. Dim StartY As Integer
  190. Dim StartLength As Integer
  191. ' ************************************************
  192. ' Recursively draw a tree branch.
  193. ' ************************************************
  194. Sub DrawBranch(bend As Single, thickness As Integer, level As Integer, x As Integer, y As Integer, length As Integer, theta As Single)
  195. Const DIST_PER_BEND = 5#
  196. Const BEND_FACTOR = 2#
  197. Const MAX_BEND = PI / 6
  198. Dim x1 As Integer
  199. Dim y1 As Integer
  200. Dim x2 As Integer
  201. Dim y2 As Integer
  202. Dim status As Integer
  203. Dim num_bends As Integer
  204. Dim num_branches As Integer
  205. Dim i As Integer
  206. Dim new_length As Integer
  207. Dim new_theta As Single
  208. Dim new_bend As Single
  209. Dim dt As Single
  210. Dim t As Single
  211.     If thickness > 0 Then Canvas.DrawWidth = thickness
  212.     ' Draw the branch.
  213.     If bend > 0 Then
  214.         ' This is a bending branch.
  215.         num_bends = length / DIST_PER_BEND
  216.         t = theta
  217.         x1 = x
  218.         y1 = y
  219.         For i = 1 To num_bends
  220.             x2 = x1 + DIST_PER_BEND * Cos(t)
  221.             y2 = y1 + DIST_PER_BEND * Sin(t)
  222.             Canvas.Line (x1, y1)-(x2, y2)
  223.         
  224.             t = t + bend * (Rnd - 0.5)
  225.             x1 = x2
  226.             y1 = y2
  227.         Next i
  228.     Else
  229.         ' This is a straight branch.
  230.         x1 = x + length * Cos(theta)
  231.         y1 = y + length * Sin(theta)
  232.         Canvas.Line (x, y)-(x1, y1)
  233.     End If
  234.     ' If level > 1, draw the attached branches.
  235.     If level > 1 Then
  236.         num_branches = Int((MaxBranches - 1) * Rnd + 2)
  237.         dt = 2 * DTheta / (num_branches - 1)
  238.         t = theta - DTheta
  239.         For i = 1 To num_branches
  240.             new_length = length * (LengthScale + RndScale * (Rnd - 0.5))
  241.             new_theta = t + RndDTheta * (Rnd - 0.5)
  242.             t = t + dt
  243.             If bend > 0 Then
  244.                 new_bend = bend * BEND_FACTOR
  245.                 If new_bend > MAX_BEND Then new_bend = MAX_BEND
  246.             Else
  247.                 new_bend = bend
  248.             End If
  249.             DrawBranch new_bend, thickness - 1, level - 1, x1, y1, new_length, new_theta
  250.         Next i
  251.     End If
  252. End Sub
  253. Private Sub CmdGo_Click()
  254. Dim taper As Integer
  255. Dim bend As Single
  256.     Canvas.Cls
  257.     MousePointer = vbHourglass
  258.     DoEvents
  259.     ' Get the tree parameters.
  260.     If Not IsNumeric(LevelText.Text) Then _
  261.         LevelText.Text = "5"
  262.     TheLevel = CInt(LevelText.Text)
  263.     If Not IsNumeric(ScaleText.Text) Then _
  264.         ScaleText.Text = "0.75"
  265.     LengthScale = CSng(ScaleText.Text)
  266.     If Not IsNumeric(DThetaText.Text) Then _
  267.         DThetaText.Text = "36"
  268.     DTheta = CSng(DThetaText.Text) * PI / 180#
  269.     If Not IsNumeric(RndScaleText.Text) Then _
  270.         RndScaleText.Text = "0.2"
  271.     RndScale = CSng(RndScaleText.Text)
  272.     If Not IsNumeric(RndDThetaText.Text) Then _
  273.         RndDThetaText.Text = "20"
  274.     RndDTheta = CSng(RndDThetaText.Text) * PI / 180#
  275.     If Not IsNumeric(MaxBranchesText.Text) Then _
  276.         MaxBranchesText.Text = "3"
  277.     MaxBranches = CInt(MaxBranchesText.Text)
  278.     If TaperCheck.Value = vbChecked Then
  279.         taper = TheLevel
  280.     Else
  281.         taper = 0
  282.     End If
  283.     If BendCheck.Value = vbChecked Then
  284.         bend = PI / 90
  285.     Else
  286.         bend = 0
  287.     End If
  288.     StartLength = (Canvas.ScaleHeight - 10) / _
  289.         ((1 - LengthScale ^ (TheLevel + 1)) / (1 - LengthScale))
  290.     ' Draw the tree.
  291.     DrawBranch bend, taper, TheLevel, StartX, StartY, StartLength, -PI_2
  292.     MousePointer = vbDefault
  293. End Sub
  294. Private Sub Form_Load()
  295.     Randomize
  296.     TheLevel = CInt(LevelText.Text)
  297. End Sub
  298. Private Sub Form_Resize()
  299.     Canvas.Move Canvas.Left, 0, _
  300.         ScaleWidth - Canvas.Left, ScaleHeight - 1
  301.     StartX = Canvas.ScaleWidth \ 2
  302.     StartY = Canvas.ScaleHeight - 5
  303. End Sub
  304. Private Sub mnuFileExit_Click()
  305.     Unload Me
  306. End Sub
  307.